package hashcode_equals;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class EqualsDemo {

    public static void main(String[] args) {
        List<ImmutableStudent> studentList = new ArrayList<>();
        // 利用 HashSet 不能加入重复元素的特性
        Set<Object> studentSet = new HashSet<>();

        ImmutableStudent student1 = new ImmutableStudent(1, "张三");
        ImmutableStudent student2 = new ImmutableStudent(1, "张三");

        System.out.println("student1 == student2: " + (student1 == student2));
        System.out.println("student1.equals(student2): " + student1.equals(student2));
        System.out.println("student1 hashCode: " + student1.hashCode());
        System.out.println("student2 hashCode: " + student2.hashCode());

        studentList.add(student1);
        studentList.add(student2);
        System.out.println("list size: " + studentList.size());

        studentSet.add(student1);
        studentSet.add(student2);
        System.out.println("set size: " + studentSet.size());

        student1.setName("李四"); // 可以尝试把 student1 改为 student2
        boolean removingStudent1 = studentSet.remove(student1);
        System.out.println("after removing student1, removing result: " + removingStudent1 +
                ", set size: " + studentSet.size());

        boolean removingStudent2 = studentSet.remove(student2);
        System.out.println("after removing student2, removing result: " + removingStudent2 +
                ", set size: " + studentSet.size());

    }

}
